<template>
  <div class="mod-category">
    <el-form :inline="true"
             :model="dataForm">
      <el-form-item>
        <el-button type="primary"
                   icon="el-icon-plus"
                   size="small"
                   v-if="isAuth('prod:category:save')"
                   @click="addOrUpdateHandle()">{{$t("crud.addTitle")}}</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList"
              border
              row-key="categoryId"
              style="width: 100%;">
      <el-table-column prop="categoryName"
                       header-align="center"
                       treeKey="categoryId"
                       align="center"
                       width="150"
                       :label="this.$i18n.t('publics.category')">
      </el-table-column>
      <el-table-column prop="status"
                       header-align="center"
                       align="center"
                       :label="this.$i18n.t('product.status')">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0"
                  size="small"
                  type="danger">{{$t("live.offline")}}</el-tag>
          <el-tag v-else
                  size="small">{{$t("publics.normal")}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="seq"
                       header-align="center"
                       align="center"
                       :label="this.$i18n.t('hotSearch.seq')">
      </el-table-column>
      <el-table-column fixed="right"
                       header-align="center"
                       align="center"
                       :label="this.$i18n.t('text.menu')">
        <template slot-scope="scope">
          <el-button type="primary"
                     size="small"
                     v-if="isAuth('prod:category:update')"
                     @click="addOrUpdateHandle(scope.row.categoryId)">{{$t("crud.updateBtn")}}</el-button>
          <el-button type="danger"
                     size="small"
                     v-if="isAuth('prod:category:delete')"
                     @click="deleteHandle(scope.row.categoryId)">{{$t("text.delBtn")}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible"
                   ref="addOrUpdate"
                   @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './category-add-or-update'
import { treeDataTranslate } from '@/utils'

export default {
  data () {
    return {
      dataForm: {},
      searchForm: {
        categoryName: null,
        status: null
      },
      options: [
        {
          label: this.$i18n.t('publics.normal'),
          value: 1
        },
        {
          label: this.$i18n.t('live.offline'),
          value: 0
        }
      ],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      dataList: [],
      dataListLoading: false,
      addOrUpdateVisible: false,
      resourcesUrl: window.SITE_CONFIG.resourcesUrl
    }
  },
  components: {
    AddOrUpdate
  },
  created () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/prod/category/listCategory'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.dataList = treeDataTranslate(data, 'categoryId', 'parentId')
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle (id) {
      this.$confirm(`${this.$i18n.t('sys.makeSure')}[${this.$i18n.t('text.delBtn')}]${this.$i18n.t('text.menu')}?`, this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('crud.filter.submitBtn'),
        cancelButtonText: this.$i18n.t('crud.filter.cancelBtn'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl(`/prod/category/${id}`),
          method: 'delete',
          data: this.$http.adornData()
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList()
            }
          })
        })
      })
    }
  }
}
</script>
<style lang="scss">
.mod-category {
  img {
    height: 80px;
    width: 200px;
  }
}
</style>
